package com.huidu.bitrade.config;

import com.huidu.bitrade.util.BigDecimalToDecimal128Converter;
import com.huidu.bitrade.util.Decimal128ToBigDecimalConverter;
//import com.mongodb.Mongo;
//import com.mongodb.MongoClient;
//import com.mongodb.MongoClientURI;

import com.mongodb.client.MongoClient;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.MongoDbFactory;
//import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
//import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

import java.util.ArrayList;
import java.util.List;

@Configuration
@ConditionalOnProperty(name="spring.data.mongodb.uri")
@AllArgsConstructor
public class MongoConfig {

//    @Value("${spring.data.mongodb.uri}")
//    private String uri ;

//    @Override
//    protected String getDatabaseName() {
//        return getMongoClientURI().getDatabase();
//    }

//    @Override
//    public Mongo mongo() {
//        MongoClient mongoClient = new MongoClient(this.getMongoClientURI());
//        return mongoClient;
//    }

//    public MongoClientURI getMongoClientURI(){
//        return new MongoClientURI(uri);
//    }
//
//    @Override
//    @Bean
//    public MappingMongoConverter mappingMongoConverter() throws Exception {
//        DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(this.dbFactory());
//        MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, this.mongoMappingContext());
//        List<Object> list = new ArrayList<>();
//        list.add(new BigDecimalToDecimal128Converter());//自定义的类型转换器
//        list.add(new Decimal128ToBigDecimalConverter());//自定义的类型转换器
//        //list.add(new DateLocalConvert());
//        converter.setCustomConversions(new CustomConversions(list));
//        return converter;
//    }
//    @Bean
//    public MongoDbFactory dbFactory() throws Exception {
//        return new SimpleMongoDbFactory(getMongoClientURI());
//    }
//    @Override
//    @Bean
//    public MongoMappingContext mongoMappingContext() {
//        return new MongoMappingContext();
//    }

//    @Override
//    public MongoClient mongoClient() {
//        MongoClient mongoClient = new MongoClient(this.getMongoClientURI());
//        return mongoClient;
//    }

//    @Override
//    @Bean(name="newMongoTemplate")
//    public MongoTemplate mongoTemplate() throws Exception {
//        return new MongoTemplate(this.dbFactory(), this.mappingMongoConverter());
//    }

    private final MongoProperties mongoProperties;

    @Bean
    public MongoDatabaseFactory dbFactory(MongoProperties mongoProperties) {
        return new SimpleMongoClientDatabaseFactory(mongoProperties.getUri());
    }

    @Bean
    public MongoTemplate mongoTemplate() {
        MongoTemplate mongoTemplate=new MongoTemplate(dbFactory(mongoProperties));
        MappingMongoConverter mongoMapping = (MappingMongoConverter) mongoTemplate.getConverter();
        mongoMapping.setCustomConversions(mongoCustomConversions());
        mongoMapping.afterPropertiesSet();
        return mongoTemplate;
    }

    @Bean
    public MongoCustomConversions mongoCustomConversions() {
        List<Converter<?, ?>> converterList = new ArrayList<>();
        converterList.add(new BigDecimalToDecimal128Converter());//自定义的类型转换器
        converterList.add(new Decimal128ToBigDecimalConverter());//自定义的类型转换器
        return new MongoCustomConversions(converterList);
    }
}
